#!/usr/bin/env python3
# -*- coding: utf-8 -*-
help(abs)
abs(-1.1)
max(-1, 2, 3, 1)

int('123')
int(0xff)
hex(1000)
float(1)
str(123)
bool(-1)
bool(0)

m = min
m(0, -1)


def do_nothing():
    pass

def return_pos(x, y):
    a = x + 1
    b = y + 2
    return a, b

val = return_pos(1, 2) #return is a tuple
print(val)
x, y = return_pos(2, 3)
print(x, y)

def say_hello(name):
    if not isinstance(name, str):
        raise TypeError('param should be string')
    print("hello %s" % name)

say_hello('jingks')
#say_hello(1)

#可变参数
def calc(*num):
    sum = 0
    for n in num:
        sum += n*n
    return sum

calc(1, 2, 3)
calc(2, 3, 4, 5)

#关键字参数
def person(name, age, **kw):
    print("name: %s\nage: %d" % (name, age))
    for k, w in kw.items():
        print(k, ': ', w)
    if 'hobby' in kw:
        print('your have a great hobby')

kw = {'hobby': 'eat', 'goodat': 'fuck'}
person('jingks', 18, **kw)   

#命名关键字参数
def student(name, age, *, city, job):
    print(name, age, city, job)

student('jingks', 24, city = 'lh', job = 'prog')

def teacher(name, age, *param, city, job):
    print(name, age, param, city, job)

param = (1, 2, 3)
other = {'city': 'lh', 'job': 'prog'}
teacher('rey', 32, *param, **other)

#参数定义的顺序必须是：必选参数、默认参数、可变参数、命名关键字参数和关键字参数

#递归函数
def fact(n):
    if n == 1:
        return 1
    else:
        return n * fact(n - 1)


def fact2(n):
    return fact_iter(n, 1)

def fact_iter(num, pro):
    if num == 1:
        return pro
    return fact_iter(num - 1, num * pro)

#python没有实现尾递归优化
#fact2(1000)